﻿using System;
using System.IO;
using System.Reflection;

namespace SharePoint.Orm.Helpers {
	/// <summary>
	/// Helpers methods for working with a <see cref="Assembly"/>
	/// </summary>
	public static class AssemblyHelper {
		///<summary>
		/// Reads a resource from an assembly as a string.
		///</summary>
		///<param name="assembly">The assembly to read the resource from</param>
		///<param name="name">The name of the resource to read</param>
		///<returns>The resource as a string</returns>
		///<exception cref="NullReferenceException">If the resource does not exist or cannot be read</exception>
		public static string GetManifestResourceStreamAsString(this Assembly assembly, string name) {
			using (Stream stream = assembly.GetManifestResourceStream(name)) {
				if (stream == null) {
					throw new NullReferenceException("Could not find resource '" + name + "'");
				}
				using (StreamReader reader = new StreamReader(stream)) {
					return reader.ReadToEnd();
				}
			}
		}
	}
}
